<template>
  <PageListContainer>
    <div class="base-block">
      <k-search :model="state.params" @search="methods.onSearch" @reset="onClickReset">
        <el-form-item label="付款时间">
          <el-date-picker
            v-model="state.params._timeArr"
            type="daterange"
            format="YYYY-MM-DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :shortcuts="dateShortcut"
            @change="methods.onSearch"
          />
        </el-form-item>
        <el-form-item label="搜索">
          <el-input
            v-model="state.params.keyword"
            clearable
            placeholder="请输入代理商名称/群峰订单ID/大赢加手机号"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="打款状态">
          <el-select v-model="state.params.commission_status" placeholder="不限">
            <el-option
              v-for="item in commissionStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="会员规格">
          <el-select v-model="state.params.product" placeholder="不限">
            <el-option
              v-for="item in treeDataOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户类型">
          <el-select v-model="state.params.user_type" placeholder="不限">
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="代理筛选">
          <el-select
            v-model="state.params.colonel_id"
            filterable
            placeholder="不限"
          >
            <el-option
              v-for="item in agentOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </k-search>
    </div>
    <div class="base-block">
      <KToolbar>
        <template #right>
          <el-button type="default" :icon="Download" @click="onClickExportData">导出数据</el-button>
        </template>
      </KToolbar>

      <KTable v-loading="state.loading" v-bind="tableState" v-on="tableEvent">
        <template #settle_fee="s">
          <div class="table-text">
            {{ s.row.settle_fee }}
            <span :class="s.row.commission_status === 1 ? 'settled' : 'unsettled'"> {{ s.row.commission_status === 1 ? '已结算' : '未结算' }} </span>
          </div>
        </template>

        <template #remark="s">
          <div class="table-text">
            <span v-if="s.row.remark">{{ s.row.remark }}</span>
            <el-link type="primary" @click="onClickEditRemark(s.row)">编辑</el-link>
          </div>
        </template>
      </KTable>
      <!-- 订单备注 -->
      <EditRemarkModal ref="editRemarkModal" @success="methods.onSearch" />
    </div>
  </PageListContainer>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { getColonelList } from '@/api/crm-agency/dyj/colonel';
import { orderListExport } from '@/api/crm-agency/download';
import { Download } from '@element-plus/icons-vue';
import { useTableList, commissionStatusOptions, treeDataOptions, typeOptions } from './hooks/use-table';
import EditRemarkModal from './components/edit-remark-modal.vue';
import { toolGetDateRangeShorcuts as dateShortcut } from '@/utils';

defineOptions({ name: 'AgencyDyjAgentAgentOrder' });
defineProps({
  id: {
    type: [String, Number],
    default: ''
  }
});
const { state, tableState, tableEvent, ...methods } = useTableList({});
const onClickExportData = () => {
  orderListExport(methods.getParams(state.params).data);
};
// 编辑
const editRemarkModal = ref();
const onClickEditRemark = (row: any) => {
  editRemarkModal.value.show(1, row);
};
// 重置
const onClickReset = () => {
  methods.onReset();
};

/**
 * @代理商接口
 */
const agentOptions:any[] = reactive([{ label: '不限', value: -1 }]);
const getAgentList = async () => {
  const res = await getColonelList(
    {
      params: {
        page: 1,
        limit: 999
      },
      data: {
        extend_type: 2
      }
    }
  );
  if (!res.data) return;
  agentOptions.length = 0;
  agentOptions.push({ label: '不限', value: -1 });
  const items = res.data?.items || [] ;
  items.forEach((item: Record<string, any>) => {
    agentOptions.push({
      ...item,
      value: item.colonel_id,
      label: item.name
    });
  });
};

getAgentList();
</script>
<style lang="scss" scoped>

.settled {
  color: #22b20a;
}

.unsettled {
  color: #999;
}

.table-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  word-break: break-all;
}
</style>
